package com.imooc.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * @Description:
 * @Auther: sixbing  QQ:414409451
 * @Date: 2018/12/14 10:44
 */
public class FileUtil {

    public static boolean parseImmediately(MultipartFile file, String savePath, String fileName) {
//        File dest = new File("F:/test/catch/" + fileName);
        File outFile = new File(savePath + fileName);
        if (!outFile.getParentFile().exists()) { //判断文件父目录是否存在
            outFile.getParentFile().mkdir();
        }
        try {
            InputStream is = file.getInputStream();
            OutputStream os = new FileOutputStream(outFile);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int t = 0;
            while ((t = is.read(buffer)) >= 0) {
                bos.write(buffer, 0, t);
                bos.flush();
            }
            byte[] content = bos.toByteArray();
            String originalString = new String(content, "utf-8");
            System.out.println(originalString);
            is = new ByteArrayInputStream(originalString.getBytes());
            int len = 0;
            while ((len = is.read(buffer)) != -1) {
                os.write(buffer, 0, len);
            }
            is.close();
            os.close();
            return true;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    }
}
